package com.linsen.core.redis.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.linsen.core.common.constant.AuthConstant;
import com.linsen.core.common.exception.BaseException;
import com.linsen.core.common.model.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

import java.time.Duration;
import java.util.List;

/**
 * @author zhaoyong
 * @date 2023/3/20
 */
@Slf4j
@RequiredArgsConstructor
public class RedisSecurityUtil {
    private final RedisUtil<User> redisUserUtil;

    private final RedisUtil<List<String>> redisListUtil;

    public void loginAfterProcess(String token, User user, List<String> roleList, List<String> permissionList) {
        this.setUser(token, user);
        this.setRoleList(token, roleList);
        this.setPermissionList(token, permissionList);
    }

    public void logoutBeforeProcess(String token) {
        this.removeUser(token);
        this.removeRoleList(token);
        this.removePermissionList(token);
    }

    public User getUser(String token) {
        if (StrUtil.isBlank(token)) {
            throw new BaseException(HttpStatus.UNAUTHORIZED,"登录未授权");
        }

        User user = this.redisUserUtil.getOpsForValue().get(AuthConstant.USER_CACHE_PREFIX + token);
        if (ObjUtil.isNull(user)){
            throw new BaseException(HttpStatus.UNAUTHORIZED,"登录已过期");
        }

        return user;
    }

    public void setUser(String token, User user) {
        if (StrUtil.isBlank(token) || ObjUtil.isNull(user)) {
            return;
        }

        this.redisUserUtil.getOpsForValue().set(AuthConstant.USER_CACHE_PREFIX + token, user, Duration.ofSeconds(AuthConstant.TOKEN_TIMEOUT));
    }

    public void removeUser(String token) {
        if (StrUtil.isBlank(token)) {
            return;
        }

        this.redisUserUtil.getRedisTemplate().delete(AuthConstant.USER_CACHE_PREFIX + token);
    }

    public List<String> getRoleList(String token) {
        if (StrUtil.isBlank(token)) {
            return null;
        }

        return this.redisListUtil.getOpsForValue().get(AuthConstant.ROLE_CACHE_PREFIX + token);
    }

    public void setRoleList(String token, List<String> roleList) {
        if (StrUtil.isBlank(token) || CollUtil.isEmpty(roleList)) {
            return;
        }

        this.redisListUtil.getOpsForValue().set(AuthConstant.ROLE_CACHE_PREFIX + token, roleList, Duration.ofSeconds(AuthConstant.TOKEN_TIMEOUT));
    }

    public void removeRoleList(String token) {
        if (StrUtil.isBlank(token)) {
            return;
        }

        this.redisListUtil.getRedisTemplate().delete(AuthConstant.ROLE_CACHE_PREFIX + token);
    }

    public List<String> getPermissionList(String token) {
        if (StrUtil.isBlank(token)) {
            return null;
        }

        return this.redisListUtil.getOpsForValue().get(AuthConstant.PERMISSION_CACHE_PREFIX + token);
    }

    public void setPermissionList(String token, List<String> permissionList) {
        if (StrUtil.isBlank(token) || CollUtil.isEmpty(permissionList)) {
            return;
        }

        this.redisListUtil.getOpsForValue().set(AuthConstant.PERMISSION_CACHE_PREFIX + token, permissionList, Duration.ofSeconds(AuthConstant.TOKEN_TIMEOUT));
    }

    public void removePermissionList(String token) {
        if (StrUtil.isBlank(token)) {
            return;
        }

        this.redisListUtil.getRedisTemplate().delete(AuthConstant.PERMISSION_CACHE_PREFIX + token);
    }
}
